package com.ruoyi.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.exam.domain.entity.exentity.WrongQuestionDailyStatDTO;
import com.ruoyi.exam.domain.entity.exentity.WrongQuestionStatDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.entity.TWrongQuestion;
import com.ruoyi.exam.service.ITWrongQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 错题本Controller
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/exam/question/wrong")
public class TWrongQuestionController extends BaseController {
    @Autowired
    private ITWrongQuestionService tWrongQuestionService;

    /**
     * 查询错题本列表
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWrongQuestion tWrongQuestion) {
        startPage();
        List<TWrongQuestion> list = tWrongQuestionService.selectTWrongQuestionList(tWrongQuestion);
        return getDataTable(list);
    }

    /**
     * 导出错题本列表
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:export')")
    @Log(title = "错题本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TWrongQuestion tWrongQuestion) {
        List<TWrongQuestion> list = tWrongQuestionService.selectTWrongQuestionList(tWrongQuestion);
        ExcelUtil<TWrongQuestion> util = new ExcelUtil<TWrongQuestion>(TWrongQuestion.class);
        util.exportExcel(response, list, "错题本数据");
    }

    /**
     * 获取错题本详细信息
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tWrongQuestionService.selectTWrongQuestionById(id));
    }

    /**
     * 新增错题本
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:add')")
    @Log(title = "错题本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWrongQuestion tWrongQuestion) {
        return toAjax(tWrongQuestionService.insertTWrongQuestion(tWrongQuestion));
    }

    /**
     * 修改错题本
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:edit')")
    @Log(title = "错题本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWrongQuestion tWrongQuestion) {
        return toAjax(tWrongQuestionService.updateTWrongQuestion(tWrongQuestion));
    }

    /**
     * 删除错题本
     */
    // @PreAuthorize("@ss.hasPermi('exam:question:remove')")
    @Log(title = "错题本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tWrongQuestionService.deleteTWrongQuestionByIds(ids));
    }
    @GetMapping("/wrong-question")
    public TableDataInfo getWrongQuestionStatistics() {
        List<WrongQuestionStatDTO> stats = tWrongQuestionService.selectWrongQuestionStatistics();
        return getDataTable(stats);
    }
    @GetMapping("/wrong-daily-question")
    public TableDataInfo getDailyWrongQuestionStats() {
        List<WrongQuestionDailyStatDTO> list = tWrongQuestionService.selectWrongQuestionDailyStatistics();
        return getDataTable(list);
    }
}
